package com.jiangxiaofei.citycloud.mvp;

import android.content.Context;
import android.widget.Toast;

import com.blankj.utilcode.util.EmptyUtils;
import com.jiangxiaofei.citycloud.bean.FreshFruitCassifyBean;
import com.jiangxiaofei.citycloud.bean.FreshFruitHomeBean;
import com.jiangxiaofei.citycloud.bean.GoodDetailBean;
import com.jiangxiaofei.citycloud.bean.GoodIdBean;
import com.jiangxiaofei.citycloud.bean.GoodListBean;
import com.jiangxiaofei.citycloud.bean.NeedBean;
import com.jiangxiaofei.citycloud.httpUtil.OnLoadDataIm;
import com.jiangxiaofei.citycloud.param.FleaGoodListParam;
import com.jiangxiaofei.citycloud.param.FleaNeedListParam;
import com.jiangxiaofei.citycloud.param.GoodsPublicParam;
import com.jiangxiaofei.citycloud.param.PutNeedParam;
import com.jiangxiaofei.citycloud.param.SearchParam;

import java.util.List;

/**
 * @author: JiangXFei
 * @date: 2017/11/21 0021
 * @content:
 */

public class FreshPersenter implements FreshConstant.GetFreshHomePersenter,
        FreshConstant.GetFreshCassifyPersenter, FreshConstant.PutFreshGoodPersenter,
        FreshConstant.GetGoodDetailPersenter, FreshConstant.PutFreshNeedPersenter,
        FreshConstant.GetFreshNeedDetailPersenter, FreshConstant.GetFreshGoodListPersenter,
        FreshConstant.GetFreshNeedListPersenter, FreshConstant.SearchGoodListPersenter,
        FreshConstant.SearchNeedListPersenter {
    Context mContext;

    FreshConstant.GetFreshHomeModel getFreshHomeModel;
    FreshConstant.GetFreshHomeView getFreshHomeView;

    FreshConstant.GetFreshCassifyModel getFreshCassifyModel;
    FreshConstant.GetFreshCassifyView getFreshCassifyView;

    FreshConstant.PutFreshGoodModel putFreshGoodModel;
    FreshConstant.PutFreshGoodView putFreshGoodView;

    FreshConstant.GetGoodDetailModel getGoodDetailModel;
    FreshConstant.GetGoodDetailView getGoodDetailView;

    FreshConstant.PutFreshNeedModel putFreshNeedModel;
    FreshConstant.PutFreshNeedView putFreshNeedView;

    FreshConstant.GetFreshNeedDetailModel getFreshNeedDetailModel;
    FreshConstant.GetFreshNeedDetailView getFreshNeedDetailView;

    FreshConstant.GetFreshGoodListModel getFreshGoodListModel;
    FreshConstant.GetFreshGoodListView getFreshGoodListView;

    FreshConstant.GetFreshNeedListModel getFreshNeedListModel;
    FreshConstant.GetFreshNeedListView getFreshNeedListView;

    FreshConstant.SearchGoodListModel searchGoodListModel;
    FreshConstant.SearchGoodListView searchGoodListView;

    FreshConstant.SearchNeedListModel searchNeedListModel;
    FreshConstant.SearchNeedListView searchNeedListView;

    public FreshPersenter(Context mContext, FreshConstant.SearchGoodListView searchGoodListView) {
        this.mContext = mContext;
        this.searchGoodListView = searchGoodListView;
        this.searchGoodListModel = new FreshModel();
    }

    public FreshPersenter(Context mContext, FreshConstant.SearchNeedListView searchNeedListView) {
        this.mContext = mContext;
        this.searchNeedListView = searchNeedListView;
        this.searchNeedListModel = new FreshModel();
    }

    public FreshPersenter(Context mContext, FreshConstant.GetFreshGoodListView getFreshGoodListView) {
        this.mContext = mContext;
        this.getFreshGoodListView = getFreshGoodListView;
        this.getFreshGoodListModel = new FreshModel();
    }

    public FreshPersenter(Context mContext, FreshConstant.GetFreshNeedListView getFreshNeedListView) {
        this.mContext = mContext;
        this.getFreshNeedListView = getFreshNeedListView;
        this.getFreshNeedListModel = new FreshModel();
    }

    public FreshPersenter(Context mContext, FreshConstant.PutFreshNeedView putFreshNeedView) {
        this.mContext = mContext;
        this.putFreshNeedView = putFreshNeedView;
        this.putFreshNeedModel = new FreshModel();
    }

    public FreshPersenter(Context mContext, FreshConstant.GetFreshNeedDetailView getFreshNeedDetailView) {
        this.mContext = mContext;
        this.getFreshNeedDetailView = getFreshNeedDetailView;
        this.getFreshNeedDetailModel = new FreshModel();
    }

    public FreshPersenter(Context mContext, FreshConstant.GetGoodDetailView getGoodDetailView) {
        this.mContext = mContext;
        this.getGoodDetailView = getGoodDetailView;
        this.getGoodDetailModel = new FreshModel();
    }

    public FreshPersenter(Context mContext, FreshConstant.PutFreshGoodView putFreshGoodView) {
        this.mContext = mContext;
        this.putFreshGoodView = putFreshGoodView;
        this.putFreshGoodModel = new FreshModel();
    }

    private void toast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    public FreshPersenter(Context mContext, FreshConstant.GetFreshHomeView getFreshHomeView) {
        this.mContext = mContext;
        this.getFreshHomeView = getFreshHomeView;
        this.getFreshHomeModel = new FreshModel();
    }


    public FreshPersenter(Context mContext, FreshConstant.GetFreshCassifyView getFreshCassifyView) {
        this.mContext = mContext;
        this.getFreshCassifyView = getFreshCassifyView;
        this.getFreshCassifyModel = new FreshModel();
    }

    /**
     * 获取果蔬主页
     */
    @Override
    public void getFreshHome() {
        getFreshHomeModel.getFreshHome(mContext, new OnLoadDataIm() {
            @Override
            public void onSuccess(Object o, int resultCode) {

            }

            @Override
            public void onSuccess(Object o, String message, int resultCode) {
                if (EmptyUtils.isNotEmpty(o)) {
                    getFreshHomeView.getFreshHomeOnSuccess((List<FreshFruitHomeBean>) o);
                }
            }

            @Override
            public void onFailure(String msg, int resultCode) {
                toast(msg);
            }
        });
    }

    /**
     * 获取果蔬全部分类
     */

    @Override
    public void getFreshCassify() {
        int id = getFreshCassifyView.getFreshCassifyId();
        getFreshCassifyModel.getFreshCassify(mContext, id, new OnLoadDataIm() {
            @Override
            public void onSuccess(Object o, int resultCode) {

            }

            @Override
            public void onSuccess(Object o, String message, int resultCode) {
                if (EmptyUtils.isNotEmpty(o)) {
                    getFreshCassifyView.getFreshCassifyOnSuccess((List<FreshFruitCassifyBean>) o);
                }
            }

            @Override
            public void onFailure(String msg, int resultCode) {
                toast(msg);
            }
        });
    }

    /**
     * 发布果蔬商品
     */
    @Override
    public void putFreshGood() {
        GoodsPublicParam param = putFreshGoodView.getFreshGoodParam();
        if (EmptyUtils.isNotEmpty(param)) {
            putFreshGoodModel.PutFreshGood(mContext, param, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (EmptyUtils.isNotEmpty(o)) {
                        putFreshGoodView.putFreshGoodOnSuccess((List<GoodIdBean>) o);
                        toast(message);
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    toast(msg);
                }
            });
        }
    }

    /**
     * 获取果蔬商品详情页
     */
    @Override
    public void getGoodDetail() {
        int id = getGoodDetailView.getGoodDetailId();
        if (id != -1) {
            getGoodDetailModel.getGoodDetail(mContext, id, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (EmptyUtils.isNotEmpty(o)) {
                        GoodDetailBean goodDetailBean = ((List<GoodDetailBean>) o).get(0);
                        getGoodDetailView.getGoodDetailSuccess(goodDetailBean);
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    toast(msg);
                }
            });
        }
    }

    @Override
    public void putFreshhNeed() {
        PutNeedParam putNeedParam = putFreshNeedView.PutFreshNeedParam();
        if (EmptyUtils.isNotEmpty(putNeedParam)) {
            putFreshNeedModel.putFreshNeed(mContext, putNeedParam, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (EmptyUtils.isNotEmpty(o)) {
                        List<GoodIdBean> list = (List<GoodIdBean>) o;
                        if (list.size() > 0) {

                            putFreshNeedView.PutNeedSuccess(list.get(0));
                        }
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    toast(msg);
                }
            });
        }
    }

    @Override
    public void getNeedDetail() {
        int id = getFreshNeedDetailView.getNeedDetailId();
        if (id != -1) {
            getFreshNeedDetailModel.getNeedDetail(mContext, id, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (EmptyUtils.isNotEmpty(o)) {
                        List<NeedBean> list = (List<NeedBean>) o;
                        if (list.size() > 0) {
                            getFreshNeedDetailView.getNeedSuccess(list.get(0));
                        }
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {

                }
            });
        }
    }

    /**
     * 获取商品列表
     */
    @Override
    public void getGoodList() {
        FleaGoodListParam goodListParam = getFreshGoodListView.getGoodListParam();
        if (EmptyUtils.isNotEmpty(goodListParam)) {
            getFreshGoodListModel.getGoodList(mContext, goodListParam, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (EmptyUtils.isNotEmpty(o)) {
                        getFreshGoodListView.getListOnSuccess((List<GoodListBean>) o);
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {

                }
            });
        }
    }

    /**
     * 获取需求列表
     */
    @Override
    public void getNeedList() {
        FleaNeedListParam needListParam = getFreshNeedListView.getNeedListParam();
        if (EmptyUtils.isNotEmpty(needListParam)) {
            getFreshNeedListModel.getNeedList(mContext, needListParam, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (EmptyUtils.isNotEmpty(o)) {
                        getFreshNeedListView.getNeedListSuccess((List<NeedBean>) o);
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {

                }
            });
        }
    }

    /**
     * 搜索商品
     */
    @Override
    public void searchGoods() {
        SearchParam searchParam = searchGoodListView.getSearchParam();
        if (EmptyUtils.isNotEmpty(searchParam)) {
            searchGoodListModel.searchGoods(mContext, searchParam, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (EmptyUtils.isNotEmpty(o)) {
                        searchGoodListView.searchGoodsSuccess((List<GoodListBean>) o);
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    toast(msg);
                }
            });
        }

    }

    /**
     * 搜索需求
     */
    @Override
    public void SearchNeeds() {
        SearchParam searchNeedsParam = searchNeedListView.getSearchNeedsParam();
        if (EmptyUtils.isNotEmpty(searchNeedsParam)) {
            searchNeedListModel.searchNeeds(mContext, searchNeedsParam, new OnLoadDataIm() {
                @Override
                public void onSuccess(Object o, int resultCode) {

                }

                @Override
                public void onSuccess(Object o, String message, int resultCode) {
                    if (EmptyUtils.isNotEmpty(o)) {
                        searchNeedListView.searchNeedsSuccess((List<NeedBean>) o);
                    }
                }

                @Override
                public void onFailure(String msg, int resultCode) {
                    toast(msg);
                }
            });
        }

    }
}
